package http;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * 
 *
 * @author 		yuqi<307530884@qq.com> <2017年4月12日>
 * @version 	1.0
 * @since		1.0
 */
public class UploadFile {
	
    public static void main(String[] args){  
        try{  
            String boundary = "Boundary-b1ed-4060-99b9-fca7ff59c113"; //Could be any string  
            String Enter = "\r\n";  
              
            File file = new File("C:\\Users\\yu\\Desktop\\17fc00042a7ab3a2b57b.gif");  
            FileInputStream fis = new FileInputStream(file);  
              
            URL url = new URL("http://10.132.20.132:8087/hsbd/api/file/upload");  
            HttpURLConnection conn = (HttpURLConnection)url.openConnection();  
            conn.setDoOutput(true);  
            conn.setDoInput(true);  
            conn.setRequestMethod("POST");  
            conn.setUseCaches(false);  
            conn.setInstanceFollowRedirects(true);  
            conn.setRequestProperty("Content-Type","multipart/form-data;boundary=" + boundary);   
              
            conn.connect();  
              
            DataOutputStream dos = new DataOutputStream(conn.getOutputStream());  
            
          //part 1  
            String part1 =  "--" + boundary + Enter  
                    + "Content-Type: application/octet-stream" + Enter  
                    + "Content-Disposition: form-data; filename=\""+file.getName()+"\"; name=\"file\"" + Enter + Enter;  
          //part 2  
            String part2 = Enter  
                    + "--" + boundary + Enter  
                    + "Content-Type: text/plain" + Enter  
                    + "Content-Disposition: form-data; name=\"dataFormat\"" + Enter + Enter  
                    + "hk" + Enter  
                    + "--" + boundary + "--";  
              
            byte[] fielBytes = new byte[fis.available()];  
            fis.read(fielBytes);  
              
            dos.writeBytes(part1);  
            dos.write(fielBytes);  
            dos.writeBytes(part2);  
              
            dos.flush();  
            dos.close();  
            fis.close();  
            
              
            System.out.println("status code: "+conn.getResponseCode());  
            
            InputStream inputStream = null;
    		HttpURLConnection httpURLConnection = conn;
            inputStream = httpURLConnection.getInputStream(); 
			
			InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
			BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

			StringBuffer buffer = new StringBuffer();
			String str = null;
			while ((str = bufferedReader.readLine()) != null) {
				buffer.append(str);
			}
			bufferedReader.close();
			inputStreamReader.close();
			
			String resultString = buffer.toString();
			System.out.println(resultString);
			inputStream.close();
            conn.disconnect();  
              
        }catch(Exception e){  
            e.printStackTrace();  
        }  
          
    }  

}
